﻿using Hidistro.Core;
using Hidistro.UI.Common.Controls;
using JSShop.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using wnxd.javascript;

namespace Hidistro.UI.SaleSystem.CodeBehind
{
    public class wnxd_Wait : HtmlTemplatedWebControl
    {
        protected override void AttachChildControls()
        {
            if (this.Page.Request.HttpMethod == "POST")
            {
                //CreateLog("调试信息记录", "Post成功,下一行执行Query()方法!");
                this.Page.Response.Write(this.Query().ToString());
                this.Page.Response.End();
            }
        }
        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-wnxd_Wait.html";
            }
            base.OnInit(e);
        }
        protected json Query()
        {
            string orderid = this.Page.Request["orderid"];
            string isphone = this.Page.Request["isphone"];
           
            HttpCookie cookie = null;
            if (string.IsNullOrEmpty(orderid))
            {
                cookie = this.Page.Request.Cookies["wnxd_orderid"];
                if (cookie != null) orderid = cookie.Value;
            }
            Hishop_Orders order = new Juse.Interface.UserOrder().GetModelByOrderId(orderid);
            bool b = order != null ? true : false;
            json ret = new json(new { status = b });
            if (b)
            {
                if (cookie != null)
                {
                    cookie.Expires = DateTime.Now.AddDays(-1);
                    this.Page.Response.AppendCookie(cookie);
                }
                if (string.IsNullOrEmpty(isphone))
                {
                    ret["url"] = Globals.GetSiteUrls().UrlData.FormatUrl("FinishOrder", new object[] { orderid });
                }
                else
                {
                    ret["url"] = "/Vshop/FinishOrder.aspx?orderId=" + orderid;
                }
            }
            return ret;
        }
        /// <summary>
        /// 日志生成方法
        /// </summary>
        /// <param name="strtype">信息类型</param>
        /// <param name="myStr">日志内容</param>
        public void CreateLog(string strtype, string myStr)
        {
            StringBuilder str = new StringBuilder();
            str.Append("\r\n" + DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss"));
            str.Append("\r\n" + strtype);
            string ip = "";
            if (this.Page.Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR") != null)
            {
                ip = this.Page.Request.ServerVariables.Get("HTTP_X_FORWARDED_FOR").ToString().Trim();
            }
            else
            {
                ip = this.Page.Request.ServerVariables.Get("Remote_Addr").ToString().Trim();
            }
            str.Append("\r\n\tIp:" + ip);
            str.Append("\r\n\t页面：" + this.Page.Request.Url.ToString());
            str.Append("\r\n\t信息内容：" + myStr);
            str.Append("\r\n--------------------------------------------------------------------------------------------------");
            str.Append("\r\n--------------------------------------------------------------------------------------------------");
            string upLoadPath = WebConfigurationManager.AppSettings["LogPath"];
            if (!Directory.Exists(upLoadPath))
            {
                Directory.CreateDirectory(upLoadPath);
            }
            string FileName;
            string[] flist = Directory.GetFiles(upLoadPath, DateTime.Now.ToString("yyyy.MM.dd") + "_?.log");
            if (flist != null && flist.Length > 0)
            {
                FileName = upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + "_" + flist.Length.ToString() + ".log";
                FileInfo fileInfo = new FileInfo(FileName);
                if (fileInfo != null && fileInfo.Length >= 100 * 1024 * 1024)
                {
                    FileName = upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + "_" + (flist.Length + 1).ToString() + ".log";
                }
            }
            else
            {
                FileName = upLoadPath + DateTime.Now.ToString("yyyy.MM.dd") + "_1.log";
            }
            File.AppendAllText(FileName, str.ToString(), Encoding.UTF8);
        }
    }
}
